/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.iec.edp.caf.data.config;

import io.iec.caf.data.jpa.repository.support.event.CafJpaRepositoryPublish;
import io.iec.edp.caf.commons.transaction.Impl.DBTransactionServiceImpl;
import io.iec.edp.caf.commons.transaction.repository.EcpDBTransactionWarningRepository;
import io.iec.edp.caf.commons.transaction.service.DBTransactionService;
import io.iec.edp.caf.data.filter.CAFDataFilter;
import io.iec.edp.caf.data.orm.jpa.CAFImplicitNamingStrategy;
import io.iec.edp.caf.data.orm.jpa.CAFPhysicalNamingStrategy;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

/**
 * This is {@link CAFPersistenceAutoConfiguration}.
 *
 * @author yisiqi
 * @since 1.0.0
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({
        GSPCloudDatabaseConfigurationProperties.class
})
@EntityScan({"io.iec.edp.caf.commons.transaction.entity"})
@EnableJpaRepositories(basePackages = {"io.iec.edp.caf.commons.transaction.repository"})
public class CAFPersistenceAutoConfiguration {

    @Bean
    @ConditionalOnProperty("caf-boot.persistence.gsp-cloud.compatibility-mode")
    public CAFImplicitNamingStrategy cafImplicitNamingStrategy() {
        return new CAFImplicitNamingStrategy();
    }

    @Bean
    @ConditionalOnProperty("caf-boot.persistence.gsp-cloud.compatibility-mode")
    public CAFPhysicalNamingStrategy cafPhysicalNamingStrategy() {
        return new CAFPhysicalNamingStrategy();
    }

//    @Bean
//    @ConditionalOnProperty("caf-boot.persistence.gsp-cloud.compatibility-mode")
//    public AuditorAware<String> cafAuditorAware(ICAFContextService context) {
//        return new AuditorAwareImpl(context);
//    }

    @Bean
    public CAFDataFilter cafDataFilter(DBTransactionService dbTransactionService) {
        return new CAFDataFilter(dbTransactionService);
    }

    @Bean
    public CafJpaRepositoryPublish cafJpaRepositoryEventPublisher(ApplicationEventPublisher eventPublisher) {
        return new CafJpaRepositoryPublish(eventPublisher);
    }

    @Bean
    public DBTransactionService cafDBTransactionService(EcpDBTransactionWarningRepository repository) {
        return new DBTransactionServiceImpl(repository);
    }

}
